% write_basic_term.pro

:- module(write_basic_term,
      [
        write_basic_term/4,         % ?Format,+Term,+FileName,+FileArgs
        write_basic_term/3,         % ?Format,+Term,+FileName
        write_basic_term/2,         % ?Format,+Term

        write_basic_type/4,         % ?Format,+Term,+FileName,+FileArgs
        write_basic_type/3,         % ?Format,+Term,+FileName
        write_basic_type/2          % ?Format,+Term
      ]).


% consult configuration file
:- (getenv('KERNEL_CONFIG',C) ; C='../config'), [C].

:- use_module(kernel_lib(kernel)).


:- style_check(-atom).          % allow long atoms

% consult each supported output format
:- [
        kernel_lib(write_basic_term_untyped),
        kernel_lib(write_basic_term_latex),
        kernel_lib(write_basic_term_graphviz),
        kernel_lib(write_basic_term_pstree),
        kernel_lib(write_basic_term_occ),

        kernel_lib(write_basic_type_plain),
        kernel_lib(write_basic_type_latex),
        kernel_lib(write_basic_type_pstree)
   ].


%------------------------------------------------------------------------------
% WRITE BASIC TERM IN SOME SPECIFIED FORMAT
%------------------------------------------------------------------------------


%   write_basic_term(+Format, +Term, +FileName)
%
%   generate and write a latex occ file describing term
%   Term is a basic term represented as a typed prolog term
%
write_basic_term(Format, Term, FileName) :-
    catch(delete_file(FileName), _, true),
    tell(FileName),
    write_basic_term(Format, Term),
    told.


%   write_basic_term(+Format, +Term, +FileName, +Encoding)
%
%   generate and write a latex occ file describing term
%   Term is a basic term represented as a typed prolog term
%
write_basic_term(Format, Term, FileName,Encoding) :-
    catch(delete_file(FileName), _, true),
    tell(FileName),
    current_output(OutStream),
    set_stream(OutStream,Encoding),
    write_basic_term(Format, Term),
    told.


%   write_basic_term(+Format, +Term)
%
%   write latex occ text describing term
%   Term is a basic term represented as a typed prolog term
%
write_basic_term(untyped, Term) :- write_basic_term_untyped(Term).
write_basic_term(latex, Term)   :- write_basic_term_latex(Term).
write_basic_term(dot, Term)     :- write_basic_term_dot(Term).
write_basic_term(pstree, Term)  :- write_basic_term_pstree(Term).
write_basic_term(occ, Term)     :- write_basic_term_occ(Term).



%------------------------------------------------------------------------------
% WRITE BASIC TYPE OF A TERM IN SOME SPECIFIED FORMAT
%------------------------------------------------------------------------------


%   write_basic_type(+Format, +Term, +FileName)
%
%   generate and write a latex occ file describing type
%   Term is a basic type represented as a typed prolog type
%
write_basic_type(Format, Term, FileName) :-
    catch(delete_file(FileName), _, true),
    tell(FileName),
    write_basic_type(Format, Term),
    told.


%   write_basic_type(+Format, +Term, +FileName, +Encoding)
%
%   generate and write a latex occ file describing type
%   Term is a basic type represented as a typed prolog type
%
write_basic_type(Format, Term, FileName,Encoding) :-
    catch(delete_file(FileName), _, true),
    tell(FileName),
    current_output(OutStream),
    set_stream(OutStream,Encoding),
    write_basic_type(Format, Term),
    told.


%   write_basic_type(+Format, +Term)
%
%   write latex occ text describing type
%   Term is a basic type represented as a typed prolog type
%
write_basic_type(plain, Term)   :- write_basic_type_plain(Term).
write_basic_type(latex, Term)   :- write_basic_type_latex(Term).
%write_basic_type(dot, Term)     :- write_basic_type_dot(Term).
write_basic_type(pstree, Term)  :- write_basic_type_pstree(Term).
%write_basic_type(occ, Term)     :- write_basic_type_occ(Term).



%------------------------------------------------------------------------------
% DEFINE PARENTHESIS STYLE FOR DIFFERENT ABSTRACTION TYPES
%------------------------------------------------------------------------------


%   abstraction_brackets(+Beta, -LeftBracket,-RightBracket)
%
%   associates beta of an alpha->beta abstraction pair with
%   the usual mathematical parenthesis of higher order logic
%
abstraction_brackets(true::bool, '{', '}') :- !.     %set
abstraction_brackets(_::nat,     '<', '>') :- !.     %multiset
abstraction_brackets(_::int,     '<', '>') :- !.     %multiset
abstraction_brackets(_::_,       '?(', ')?') :- !.   %unknown,



%------------------------------------------------------------------------------
% GENERATE ID NUMBER
%------------------------------------------------------------------------------


%   genid(+Key, -Id)
%
%   Id is the number of times that genid has been invoked on key atom Key
%
genid(Key, Id) :-
    flag(Key, OldNum, OldNum),
    NewNum is OldNum + 1,
    flag(Key, _, NewNum),
    atom_concat(Key, NewNum, Id).